package sort;

import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class Solution {
    public int[] quickSort(int[] array) {
        return quickSortArray(array, 0, array.length - 1);
    }

    private int[] quickSortArray(int[] array, int left, int right) {
        if (left < right) {
            int partitionIndex = partition(array, left, right);
            quickSortArray(array, left, partitionIndex - 1);
            quickSortArray(array, partitionIndex + 1, right);
        }
        return array;
    }

    private int partition(int[] array, int left, int right) {
        int pivot = left;
        int index = pivot + 1;
        for (int i = index; i <= right; i++) {
            if (array[i] < array[pivot]) {
                swap(array, i, index);
                index++;
            }
        }
        swap(array, pivot, index - 1);
        return index - 1;

    }

    private void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void main(String[] args) {
        System.out.println(2 * 0.01 == 0.02);
        int[] array = new int[]{4,3,2,1};
        new Solution().quickSort(array);
        System.out.println(Arrays.toString(array));
    }

}
